<?php

class Module_mod_ratesheets_purchase extends MY_Model 
{

	static $XLSext1 = 'xls';
	static $XLSext2 = 'xlsx';
	static $CSVext = 'csv';
	

	function __construct() {
        parent::__construct();
    }

	
	/**
	 * Upload Files csv
	 * @param string $field_name
	 * @return string 
	 */
	public function uploadCSVfile($field_name)
	{

		if($_FILES && isset($_FILES[$field_name]) && 0 == $_FILES[$field_name]['error'])
		{

		
			if($this->upload->do_upload($field_name)) //Upload file	
			{	
				
				$response['upload_csv'] = true;
				$response['upload_msg_csv'] =  lang('term_csv_uploaded');
				
				//Get numbers from file
				$upload_data = $this->upload->data();
				
				$response['full_path_csv'] = $upload_data['full_path'];
				$full_path = $upload_data['full_path'];
			
				$data = $this->_getNumbersFromFile($full_path);
			
				$ext = ltrim($upload_data['file_ext'], '.');
				
        if(self::$CSVext != $ext )
				{
					// not allowed ext type
					$response['upload_csv'] = false;
					$response['upload_msg_csv'] =  lang('bad_allowed_ext');
					
				} else {
					if($data != false && count($data) > 0)
					{
						$response['upload_csv_data'] = $data;
					}
					else
					{
						$response['upload_msg_csv']  = lang("term_no_valid_data");
						$response['upload_csv_data'] = NULL;
						$response['upload_csv'] = false;
					}
				}
				
				//delete file
				chmod($full_path, 777);
			  unlink($full_path);
			}
			else // Display upload errors
			{
				$response['upload_csv'] = false;
				$response['upload_msg_csv'] =  $this->upload->display_errors();
				
			}
		}
		else // no uploaded file
		{
			$response['upload_csv'] = false;
			$response['upload_msg_csv'] =  lang('not_upload_file');
		}	
	
		return $response;
	}

	
	/**
	 * Upload Files xls
	 * @param string $field_name
	 * @return string 
	 */
	public function uploadXLSfile($field_name)
	{
	
		if($_FILES && isset($_FILES[$field_name]) && 0 == $_FILES[$field_name]['error'])
		{
			if($this->upload->do_upload($field_name)) //Upload file	
			{	
			
				$response['upload_xls'] = true;
				$response['upload_msg_xls'] =  lang('term_xls_uploaded');
				
				//Get numbers from file
				$upload_data = $this->upload->data();
				$ext = ltrim($upload_data['file_ext'], '.');
				
				if(self::$XLSext1 != $ext && self::$XLSext2 != $ext)
				{
					// not allowed ext type
					$response['upload_xls'] = false;
					$response['upload_msg_xls'] =  lang('bad_allowed_ext');
					$response['full_path_xls'] = $upload_data['full_path'];
			
					
				} else {
					$response['full_path_xls'] = $upload_data['full_path'];
					$full_path = $upload_data['full_path'];

					if(!empty($upload_data['file_name']))
					{
						$response['upload_xls_name'] = $upload_data['file_name'];
					}
					else
					{
						$response['upload_msg_xls']  = lang("term_no_valid_data");
						$response['upload_xls_name'] = null;
						$response['upload_xls'] = false;
						$response['full_path_xls'] = $upload_data['full_path'];
					}
				}
				
	
			}
			else // Display upload errors
			{
				$response['upload_xls'] = false;
				$response['upload_msg_xls'] =  $this->upload->display_errors();
				
			}
		}
		else // no uploaded file
		{
			$response['upload_xls'] = false;
			$response['upload_msg_xls'] =  lang('not_upload_file');
		}	
		
		return $response;
	}
	
	/**
	 * 
	 * Retreive all data from file
	 * @param string $full_path - ful path to do file
	 * @return: array
	 */
	private function _getNumbersFromFile($full_path)
	{

		//Read file contents
		if(($handle = fopen($full_path, "r")) !== FALSE) {
			$row = 1;
			while (($dataCSV = fgetcsv($handle, 10000, ";")) !== FALSE) {
				$num = count($dataCSV);
        
				for ($c=0; $c < $num; $c++) {
					$data[$row][$c] = trim($dataCSV[$c]);
				}

				if(isset($data[$row]) && count($data[$row]) < 5 )  // min items in array is 3-> if not 3, data not valid 
				{
					unset($data[$row]);
				} else {}
				$row++;
			}
			fclose($handle);
		}
    
    if(count($data) == 0)
    {
      return false;
    } else {
      return $data;
    }
	}
	
	
} // end of class
?>
